﻿using System.Text;

namespace FuzzScript
{
    interface IConsequentClause
    {
        void InferFuzzyOutput(double p_dDoM);
    }

    class ConsequentSetClause : SetClause, IConsequentClause
    {
        internal ConsequentSetClause(string p_sVariableName, string p_sSetName)
            : base(p_sVariableName, p_sSetName)
        {
            m_OutputSet = (FuzzySet)SymbolTable.Instance.Get(
                SymbolTable.AttributeOutputVariableSet(m_sVariableName));
        }

        FuzzySet m_OutputSet = null;

        public void InferFuzzyOutput(double p_dDoM)
        {
            m_OutputSet.MaxMin((FuzzySet)m_SiblingNodes[0], p_dDoM);
        }

        public override string Generate()
        {
            StringBuilder sCode = new StringBuilder();

            sCode.Append("MaxMin(");
            sCode.Append(
                SymbolTable.AttributeOutputVariableSet(VariableName));
            sCode.Append(",");
            sCode.Append(
                SymbolTable.AttributeFuzzySet(VariableName, SetName));

            return sCode.ToString();
        }
    }

    class ConsequentAssignmentClause : Node, IConsequentClause
    {
        internal ConsequentAssignmentClause(string p_sVariableName, double p_dValue)
        {
            m_dValue = p_dValue;
            m_sVariableName = p_sVariableName;

            m_OutputSet = (FuzzySet)SymbolTable.Instance.Get(
                SymbolTable.AttributeOutputVariableSet(p_sVariableName));

            m_OutputVariable = (FuzzyVariable)SymbolTable.Instance.Get(m_sVariableName);
        }

        FuzzySet m_OutputSet = null;
        double m_dValue;
        string m_sVariableName;


        FuzzyVariable m_OutputVariable = null;

        public void InferFuzzyOutput(double p_dDoM)
        {
            m_OutputSet.SetDiscreteDoM(
                m_OutputVariable.VariableToDiscreteIndex(m_dValue),
                p_dDoM);
        }


        public override string Generate()
        {
            StringBuilder sCode = new StringBuilder();

            sCode.Append("SetDiscreteDoM(");
            sCode.Append(
                SymbolTable.AttributeOutputVariableSet(m_sVariableName));
            sCode.Append(",");
            sCode.Append(
                ((FuzzyVariable)SymbolTable.Instance.Get(m_sVariableName)).VariableToDiscreteIndex(m_dValue));

            return sCode.ToString();
        }
    }
}

